package framework.classification.entities;

import java.util.ArrayList;

import framework.classification.entities.lattice.TreillisI;
import framework.classification.library.AbstractClassfication;
import framework.entity.ACLComponent;
import framework.entity.ACLEntity;
import framework.entity.ACLFunction;
import framework.entity.ACLInterface;
public class ClComponentRequired extends ClAbstractEntity{

	public static String COMPONENTREQ = "Component_Required";

	private ArrayList<ACLComponent> components;
	
	


	public ClComponentRequired(String s, AbstractClassfication cl, TreillisI t,
			ArrayList<ACLComponent> a) {
		super(s, cl, t);
		
		components = a;
		createTableModel();
		int x = this.getAttribut().size();
		int y = this.getEntity().size();
		table = new int[x][y];
		calculPair();
		
		
	}


	public void createTableModel() {

		//liste comportant toutes les Interfaces de top niveau des differentes classifications;
		ArrayList<String> topInters = new ArrayList<String>();

		//on ajoute les composants dans les colonnes
		for (ACLComponent aclcomp : components){
			getEntity().add(aclcomp.getName());
			//--------------------------- Interface Required						
			//on ajoute les interfaces requises des different composants
			for (ACLInterface aclIntereq : aclcomp.getInterfacesReq()){

				//on recupere la classification de l'interface
				//et on y ajoute toutes les interfaces comprises dans le composant
				ClInterfaceRequired clreq = getAbstractClassfication().getInterfacesRequired().get(aclIntereq.getName());

				//on sauvegarde le top de la classification
				if (!topInters.contains(clreq.getLattice().getMaxSuperEntity())){
					ACLInterface aclnointer = new ACLInterface(clreq.getLattice().getMaxSuperEntity(),ACLFunction.MODE2);
					topInters.add(aclnointer.getName());
				}

				for (String interreq : clreq.getRealReduced().keySet()){
					ACLInterface aclinter = new ACLInterface(interreq,ACLFunction.MODE2);

					if (!getAttribut().contains("Req="+aclinter.getName()))
						getAttribut().add("Req="+aclinter.getName());
				}
				for (String interreq : clreq.getAbstractReduced().keySet()){
					ACLInterface aclinter = new ACLInterface(interreq,ACLFunction.MODE2);

					if (!getAttribut().contains("Req="+aclinter.getName()))
						getAttribut().add("Req="+aclinter.getName());
				}
			}
		}
	}

	public void calculPair() {

		for (int x = 0; x < getAttribut().size(); x++) {
			for (int y = 0; y < getEntity().size(); y++) {
				if (isPair(getAttribut().get(x), getEntity().get(y), y))
					table[x][y] = 1;
			}

		}

	}

	public Boolean isPair(String attribut, String component, int y) {
		//on parle le role de l'interface attribut required ou provided
		String strole = attribut.substring(0,attribut.indexOf("="));
		String strInterAtt = attribut.substring(attribut.indexOf('=')+1);
		ACLInterface aclInterAtt = cl.getInterfacesNames().get(strInterAtt);

		//on recupere la classification de cette interface
		ClInterfaceRequired clproAtt = getAbstractClassfication().getInterfacesRequired().get(aclInterAtt.getName());

		//pour toutes les interfaces contenues dans le composant
		ArrayList<ACLInterface> interEntity = getAbstractClassfication().getStructure().getComponentsRequired().get(component);
		for (ACLInterface aclInterEntity :interEntity){

			//on regarde si l'interface du comopsant est une interface enfant de l'interface attribut
			if (clproAtt.getLattice().isParent(aclInterEntity.toString(),aclInterAtt.toString()))
				return true;
		}
		return false;


	}
	//redefinition de la fonction de substitution
	public ArrayList<String> getSubstituteElement(String element) {
		String conceptName = null;
		ArrayList<String> result = new ArrayList<String>();
		if (lattice.getRealReduced(this).containsKey(element))
			conceptName = lattice.getRealReduced(this).get(element);
		else if (lattice.getAbstractReduced(this).containsKey(element))
			conceptName = lattice.getAbstractReduced(this).get(element);
		else {
			System.err.println("ERROR:: "+element+" no Find");
			return null;
		}
		
		ArrayList<String> conceptsubs = lattice.getParentConcept(this,conceptName);
		for (String sub : conceptsubs){
			if (lattice.getConceptToReduced(this).containsKey(sub))
				for (String el : lattice.getConceptToReduced(this).get(sub))
					result.add(el);
		}
		
		return result;
	}


	public String getType() {
		// TODO Auto-generated method stub
		return COMPONENTREQ;
	}


	@Override
	public ACLEntity getACLEntity(String s) {
		// TODO Auto-generated method stub
		return null;
	}




}
